#include "component.hpp"

namespace footman {
    ComponentPool Component::__pool;
    static std::array< size_t, MaxComponents> DeleteCount;

    const size_t  MaxDeleteCount = 20;
    void Component::__PoolGc( ComponentID typeId ){
        auto& pool = Component::__pool[typeId];
        if ( DeleteCount[typeId] >= MaxDeleteCount ) {
            for( auto iter = pool.begin(); iter != pool.end();) {
                if( !Object::IsValid( iter->get() ) ) {
                    iter = pool.erase( iter );
                }
                else {
                    iter++;
                }
            }
        }
        DeleteCount[typeId] = 0;
    }

    Component::Component( ) : node( nullptr ) {
    }

    Component::~Component()
    {
    }

    void Component::destroy()
    {
        Object::destroy();
        ComponentID typeId = Component::__GetComponentTypeID<Component>();
        DeleteCount[typeId]++;
        Component::__PoolGc( typeId );
    }
}